package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.SysRoleDao;
import com.shop.admin.entity.ShopAdmin;
import com.shop.admin.entity.ShopOrder;
import com.shop.admin.entity.SysRole;
import com.shop.admin.entity.SysUserRole;
import com.shop.admin.service.SysRoleService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.vo.PostPageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Resource
    private SysRoleService sysRoleService;

    @Override
    public PageUtils getRolesList(Map<String, Object> params) {
        Long pageValue = params.get("page") == null ? 1L : Long.parseLong(params.get("page").toString());
        Long limitValue = params.get("limit") == null ? 0L : Long.parseLong(params.get("limit").toString());
        Page<SysRole> res = new Page<>(pageValue, limitValue);
        if (limitValue == 0) {
            List<SysRole> resPage = sysRoleService.list(new LambdaQueryWrapper<SysRole>().eq(SysRole::getIsDeleted,0));
            return new PageUtils(resPage,resPage.size());
        }
        List<SysRole> resPage = sysRoleService.list(new LambdaQueryWrapper<SysRole>().eq(SysRole::getIsDeleted,0));
        res.setRecords(resPage);
        res.setTotal(resPage.size());
        PageUtils pageUtils=new PageUtils(res);
        return pageUtils;
    }

    @Override
    public boolean roleUpdateBoolean(String roleName, String roleCode, Integer id) {
        int roleNameCount = this.baseMapper.selectCount((new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, roleName).ne(id != null, SysRole::getId, id)));
        int roleCodeCount = this.baseMapper.selectCount((new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode,roleCode).ne(id != null, SysRole::getId, id)));
        return roleNameCount + roleCodeCount == 0;
    }

    @Override
    public boolean roleSaveBoolean(String roleName, String roleCode) {
        int roleNameCount = this.baseMapper.selectCount(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, roleName));
        int roleCodeCount = this.baseMapper.selectCount(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode, roleCode));
        return roleNameCount + roleCodeCount == 0;
    }

    @Override
    public void removeRoleByIds(List<Long> asList) {
        for (Long aLong : asList) {
            SysRole sysRole = new SysRole();
            sysRole.setId(aLong.intValue());
            sysRole.setIsDeleted(1);
            sysRole.setUpdateTime(new Date());
            sysRoleService.updateById(sysRole);
        }
    }
}
